package com.alibaba.citrus.cr.unified.inventory.functions.allocateApplyOrder;

import com.alibaba.citrus.cr.unified.inventory.repository.AllocateApplyOrderDetailReadRepository;
import com.alibaba.citrus.cr.unified.inventory.repository.AllocateApplyOrderDetailWriteRepository;
import com.alibaba.citrus.cr.unified.inventory.repository.AllocateApplyOrderReadRepository;
import com.alibaba.citrus.cr.unified.inventory.repository.AllocateApplyOrderWriteRepository;
import com.alibaba.citrus.cr.common.util.SpringContextUtil;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.crunifiedinventory.domain.allocateapplyorderdetail.model.AllocateApplyOrderDetail;
import com.epoch.app.crunifiedinventory.sdo.AllocateApplyOrderDetailSDO;
import com.epoch.app.crunifiedinventory.sdo.AllocateApplyOrderSDO;
import com.epoch.app.crunifiedinventory.sdo.enums.AllocateApplyOrderStatusEnum;
import lombok.Getter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.List;
import java.util.Objects;

@Getter
public class AllocateApplyOrderEntity {
    private static Log log = LogFactory.getLog(AllocateApplyOrderEntity.class);

    private AllocateApplyOrderSDO allocateApplyOrderSDO;

    private static AllocateApplyOrderWriteRepository allocateApplyOrderWriteRepository;

    private static AllocateApplyOrderReadRepository allocateApplyOrderReadRepository;

    private static AllocateApplyOrderDetailWriteRepository allocateApplyOrderDetailWriteRepository;

    private static AllocateApplyOrderDetailReadRepository allocateApplyOrderDetailReadRepository;


    static {
        allocateApplyOrderWriteRepository = SpringContextUtil.getBean(AllocateApplyOrderWriteRepository.class);
        allocateApplyOrderReadRepository = SpringContextUtil.getBean(AllocateApplyOrderReadRepository.class);
        allocateApplyOrderDetailWriteRepository = SpringContextUtil.getBean(AllocateApplyOrderDetailWriteRepository.class);
        allocateApplyOrderDetailReadRepository = SpringContextUtil.getBean(AllocateApplyOrderDetailReadRepository.class);
    }

    public static AllocateApplyOrderEntity of(AllocateApplyOrderSDO allocateApplyOrderSDO) {
        AllocateApplyOrderEntity entity = new AllocateApplyOrderEntity();
        entity.allocateApplyOrderSDO=allocateApplyOrderSDO;
        return entity;
    }

    public String create() {
        log.info("Entity调拨预报单创建入参："+ JSONObject.toJSONString(allocateApplyOrderSDO));
        return allocateApplyOrderWriteRepository.createAllocateApplyOrder(allocateApplyOrderSDO);
    }

    public String nykCreate() {
        log.info("nyk create entity ："+ JSONObject.toJSONString(allocateApplyOrderSDO));
        return allocateApplyOrderWriteRepository.nykCreateAllocateApplyOrder(allocateApplyOrderSDO);
    }

    public void update() {
        //更新调拨单
        allocateApplyOrderWriteRepository.update(allocateApplyOrderSDO);
    }

    public void nykUpdate() {
        //插入详情
        List<AllocateApplyOrderDetailSDO> list = allocateApplyOrderSDO.getList();
        for (AllocateApplyOrderDetailSDO allocateApplyOrderDetailSDO:list){
            allocateApplyOrderDetailSDO.setIsDTC(Objects.isNull(allocateApplyOrderSDO.getIsDTC())?false:allocateApplyOrderSDO.getIsDTC());
        }
        allocateApplyOrderDetailWriteRepository.batchCreateAllocateApplyOrderDetail(list);
    }

    public void copyDetail() {
        //插入详情
        List<AllocateApplyOrderDetailSDO> list = allocateApplyOrderSDO.getList();
        for (AllocateApplyOrderDetailSDO allocateApplyOrderDetailSDO:list){
            allocateApplyOrderDetailSDO.setIsDTC(Objects.isNull(allocateApplyOrderSDO.getIsDTC())?false:allocateApplyOrderSDO.getIsDTC());
        }
        allocateApplyOrderDetailWriteRepository.batchCreateAllocateApplyOrderDetail(list);
    }

    public void cancel() {
        //更新调拨单
        allocateApplyOrderWriteRepository.cancel(allocateApplyOrderSDO);
    }

    public void sapUpdate() {
        allocateApplyOrderWriteRepository.sapUpdate(allocateApplyOrderSDO);
    }
    public void sapCreate() {
        allocateApplyOrderWriteRepository.sapCreate(allocateApplyOrderSDO);
    }
}
